/* This reference code does not compile with the project. */

using System;
using System.Collections.Generic;
using System.Text;

[SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel=true), SharePointPermission(SecurityAction.LinkDemand, ObjectModel=true)]
public class SPFieldMultiLineText : SPField
{
    // Fields
    private const int BROKENLINKLIMIT = 100;
    private SPPreviewValueSize previewValueSize;

    // Methods
    internal SPFieldMultiLineText(SPFieldCollection fields, int index) : base(fields, index)
    {
    }

    public SPFieldMultiLineText(SPFieldCollection fields, string fieldName) : base(fields, fieldName)
    {
    }

    public SPFieldMultiLineText(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName)
    {
    }

    public override string GetFieldValueAsHtml(object value)
    {
        return this.GetFieldValueAsHtml(value, null);
    }

    public string GetFieldValueAsHtml(object value, SPListItem item)
    {
        string str;
        if (value != null)
        {
            if (this.RichText)
            {
                str = value.ToString();
            }
            else
            {
                str = SPHttpUtility.HtmlEncode(value.ToString());
            }
        }
        else
        {
            return string.Empty;
        }
        if (!this.WikiLinking)
        {
            return str;
        }
        return this.ProcessWikiLinks(str, item);
    }

    public override string GetFieldValueAsText(object value)
    {
        if (value == null)
        {
            return string.Empty;
        }
        if (!this.RichText)
        {
            return value.ToString();
        }
        return SPHttpUtility.ConvertSimpleHtmlToText(value.ToString(), -1);
    }

    public override string GetFieldValueForEdit(object value)
    {
        if (value != null)
        {
            if (!this.WikiLinking)
            {
                if (!this.RichText)
                {
                    return this.GetFieldValueAsText(value);
                }
                return this.GetFieldValueAsHtml(value);
            }
            return SPWikiUtility.ConvertHtmlLinkToWikiLink(value.ToString(), SPContext.Current.RootFolderUrl);
        }
        return string.Empty;
    }

    public override string GetValidatedString(object value)
    {
        if (value != null)
        {
            if (this.WikiLinking && (base.ParentList != null))
            {
                uint num = (uint) (value.ToString().Length / 0x400);
                SQM.SetDpIfMax(SQMDP.DATAID_WIKIPAGESIZE, num);
                SQM.AddToAvgDp(SQMDP.DATAID_WIKIPAGESIZEAVG, num);
                return SPWikiUtility.ConvertWikiLinkToHtmlLink(value.ToString(), base.ParentList.RootFolderUrl);
            }
        }
        else
        {
            return string.Empty;
        }
        return value.ToString();
    }

    public string ProcessWikiLinks(string input, SPListItem item)
    {
        SPList parentList;
        StringBuilder builder;
        if (item != null)
        {
            parentList = item.ParentList;
        }
        else
        {
            SPContext current = SPContext.Current;
            if (current != null)
            {
                item = current.ListItem;
                parentList = current.List;
            }
            else
            {
                return input;
            }
        }
        if (item != null)
        {
            if (item.IsNew)
            {
                return input;
            }
            if (parentList != null)
            {
                Hashtable hashtable = new Hashtable();
                int num = 0;
                builder = new StringBuilder(input);
                StringBuilder builder2 = new StringBuilder();
                StringBuilder builder3 = new StringBuilder();
                builder2.Append("class=ms-wikilink href=\"");
                int length = builder2.Length;
                builder3.Append("class=ms-missinglink href=\"");
                builder3.Append(SPHttpUtility.UrlPathEncode(base.Fields.Web.Url, true));
                builder3.Append("/_layouts/CreateWebPage.aspx?List={");
                builder3.Append(SPHttpUtility.UrlKeyValueEncode(Convert.ToString(parentList.ID, CultureInfo.InvariantCulture)));
                builder3.Append("}&RootFolderUrl=");
                builder3.Append(SPHttpUtility.UrlKeyValueEncode(parentList.RootFolderUrl));
                builder3.Append("&Name=");
                int num3 = builder3.Length;
                using (IEnumerator enumerator = item.ForwardLinks.GetEnumerator())
                {
                    SPLink link;
                    string str3;
                    goto Label_00B5;
                Label_0021:
                    num++;
                    if (num > 100)
                    {
                        goto Label_02CB;
                    }
                    goto Label_007F;
                Label_002F:
                    link = (SPLink) enumerator.Current;
                    string serverRelativeUrl = link.ServerRelativeUrl;
                    if (serverRelativeUrl == null)
                    {
                        goto Label_00B5;
                    }
                    goto Label_0066;
                Label_004F:
                    hashtable.Add(serverRelativeUrl, null);
                    if (!link.IsBroken)
                    {
                        goto Label_00B5;
                    }
                    goto Label_0021;
                Label_0066:
                    serverRelativeUrl = SPHttpUtility.UrlPathEncode(serverRelativeUrl, true);
                    if (hashtable.ContainsKey(serverRelativeUrl))
                    {
                        goto Label_00B5;
                    }
                    goto Label_004F;
                Label_007F:;
                    string[] strArray = serverRelativeUrl.Split(new char[] { '/' });
                    string str2 = strArray[strArray.Length - 1];
                    if (str2.Length >= 6)
                    {
                        goto Label_017C;
                    }
                Label_00B5:
                    if (enumerator.MoveNext())
                    {
                        goto Label_002F;
                    }
                    goto Label_02CB;
                Label_00C6:
                    str3 = SPHttpUtility.UrlPathDecode(str2.Substring(0, str2.Length - 5), true);
                    builder2.Length = length;
                    builder2.Append(serverRelativeUrl);
                    builder2.Append("\"");
                    builder3.Length = num3;
                    builder3.Append(SPHttpUtility.UrlKeyValueEncode(str3));
                    builder3.Append("\" title=\"");
                    string valueToEncode = SPResource.GetString("WikiMissingLinkTitle", new object[] { str3 });
                    builder3.Append(SPHttpUtility.HtmlEncode(valueToEncode));
                    builder3.Append("\"");
                    builder = builder.Replace(builder2.ToString(), builder3.ToString());
                    goto Label_00B5;
                Label_017C:
                    if (!str2.EndsWith(".aspx"))
                    {
                        goto Label_00B5;
                    }
                    goto Label_00C6;
                }
            }
        }
        return input;
    Label_02CB:
        return builder.ToString();
    }

    protected void SetRichTextMode(SPRichTextMode mode)
    {
        string str;
        switch (mode)
        {
            case SPRichTextMode.FullHtml:
                str = "FullHtml";
                break;

            case SPRichTextMode.HtmlAsXml:
                str = "HtmlAsXml";
                break;

            default:
                str = "Compatible";
                break;
        }
        base.SetFieldAttributeValue("RichTextMode", str);
    }

    // Properties
    public bool AllowHyperlink
    {
        get
        {
            return base.GetFieldBoolValue("AllowHyperlink");
        }
        set
        {
            base.SetFieldBoolValue("AllowHyperlink", value);
        }
    }

    public bool AppendOnly
    {
        get
        {
            return base.GetFieldBoolValue("AppendOnly", 8);
        }
        set
        {
            base.SetFieldBoolValue("AppendOnly", value);
        }
    }

    public int DifferencingLimit
    {
        get
        {
            string fieldAttributeValue = base.GetFieldAttributeValue("DifferencingLimit");
            if (fieldAttributeValue != null)
            {
                return Convert.ToInt32(fieldAttributeValue, CultureInfo.InvariantCulture);
            }
            return 0x5dc;
        }
        set
        {
            if (value > 0x1388)
            {
                value = 0x5dc;
            }
            base.SetFieldAttributeValue("DifferencingLimit", Convert.ToString(value, CultureInfo.InvariantCulture));
        }
    }

    public override BaseFieldControl FieldRenderingControl
    {
        get
        {
            BaseFieldControl control;
            if (!this.RichText)
            {
                control = new NoteField();
            }
            else
            {
                control = new RichTextField();
            }
            control.FieldName = base.InternalName;
            return control;
        }
    }

    public override SPMobileBaseFieldControl FieldRenderingMobileControl
    {
        get
        {
            return new SPMobileNoteField();
        }
    }

    public override Type FieldValueType
    {
        get
        {
            return typeof(string);
        }
    }

    public override bool Filterable
    {
        get
        {
            return false;
        }
    }

    public bool IsolateStyles
    {
        get
        {
            return base.GetFieldBoolValue("IsolateStyles");
        }
        set
        {
            base.SetFieldBoolValue("IsolateStyles", value);
        }
    }

    public int NumberOfLines
    {
        get
        {
            int num;
            if (base.GetFieldIntValue("NumLines", out num))
            {
                while (num > 0)
                {
                    if (num >= 0x3e9)
                    {
                        break;
                    }
                    return num;
                }
            }
            return 6;
        }
        set
        {
            if ((value < 1) || (value > 0x3e8))
            {
                throw new ArgumentOutOfRangeException();
            }
            base.SetFieldAttributeValue("NumLines", Convert.ToString(value));
        }
    }

    public SPPreviewValueSize PreviewValueSize
    {
        get
        {
            return this.previewValueSize;
        }
        set
        {
            this.previewValueSize = value;
        }
    }

    public override object PreviewValueTyped
    {
        get
        {
            StringBuilder builder = new StringBuilder();
            if (this.RichText)
            {
                builder.Append("<div>");
            }
            builder.AppendFormat(CultureInfo.CurrentUICulture, SPResource.GetString("FieldPreviewValue", new object[0]), new object[] { base.Title });
            switch (this.PreviewValueSize)
            {
                case SPPreviewValueSize.Medium:
                    builder.Append(" " + SPResource.GetString("MultiLineFieldPreviewValueSentence", new object[0]));
                    break;

                case SPPreviewValueSize.Large:
                    builder.Append(" " + SPResource.GetString("MultiLineFieldPreviewValueParagraph", new object[0]));
                    break;

                case SPPreviewValueSize.ExtraLarge:
                {
                    string str = SPResource.GetString("MultiLineFieldPreviewValueParagraph", new object[0]);
                    builder.Append(" " + str);
                    if (!this.RichText)
                    {
                        builder.Append("\r\n\r\n");
                    }
                    else
                    {
                        builder.Append("<br/><br/>");
                    }
                    builder.Append(str);
                    break;
                }
            }
            if (this.RichText)
            {
                builder.Append("</div>");
            }
            return builder.ToString();
        }
    }

    public bool RestrictedMode
    {
        get
        {
            XmlNode namedItem = base.Node.Attributes.GetNamedItem("RestrictedMode");
            if (((namedItem != null) && (namedItem.Value != "TRUE")) && !(namedItem.Value == "-1"))
            {
                return false;
            }
            return true;
        }
        set
        {
            base.SetFieldBoolValue("AllowHyperlink", value);
        }
    }

    public bool RichText
    {
        get
        {
            return base.GetFieldBoolValue("RichText", 10);
        }
        set
        {
            base.SetFieldBoolValue("RichText", value);
        }
    }

    public virtual SPRichTextMode RichTextMode
    {
        get
        {
            string fieldAttributeValue = base.GetFieldAttributeValue("RichTextMode");
            if (fieldAttributeValue != null)
            {
                while (fieldAttributeValue == "HtmlAsXml")
                {
                    return SPRichTextMode.HtmlAsXml;
                }
                while (fieldAttributeValue == "FullHtml")
                {
                    return SPRichTextMode.FullHtml;
                }
            }
            return SPRichTextMode.Compatible;
        }
        set
        {
            if (value == SPRichTextMode.HtmlAsXml)
            {
                throw new ArgumentException(SPResource.GetString("HtmlAsXmlUnsupported", new object[0]));
            }
            this.SetRichTextMode(value);
        }
    }

    public override bool Sortable
    {
        get
        {
            return false;
        }
    }

    public bool UnlimitedLengthInDocumentLibrary
    {
        get
        {
            return base.GetFieldBoolValue("UnlimitedLengthInDocumentLibrary");
        }
        set
        {
            base.SetFieldBoolValue("UnlimitedLengthInDocumentLibrary", value);
        }
    }

    public bool WikiLinking
    {
        get
        {
            if (!this.RichText)
            {
                return false;
            }
            return base.GetFieldBoolValue("WikiLinking", 14);
        }
    }
}